﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Web.Mvc;
using System.Web.Security;
using System.Linq;
using System.Web;
using FantasyBlog.DAL;
using FantasyBlog.MemeberShip;

namespace FantasyBlog.Models
{
    public class UserModel
    {
        public UserModel()
        {
            TotalRecordCount = db.UserSet.Count();
            CurrentPage = 1;
            PageSize = 5;
            SortBy = "ID";
            SortAscending = true;
            list = db.UserSet.ToList();
        }

        public void getUsers()
        {
            var tempTable = db.UserSet.Where(u => u.username.Contains(UserName));
            TotalRecordCount = tempTable.Count();
            tempTable = tempTable.OrderBy(SortBy, SortAscending).Skip((CurrentPage - 1) * PageSize).Take(PageSize);
            list = tempTable.ToList();
        }

        public IEnumerable<FantasyBlog.DAL.User> FilterUsers()
        {
            var tempTable = db.UserSet.Where(u => u.username.Contains(UserName));
            TotalRecordCount = tempTable.Count();
            list = tempTable.ToList();
            return list;
        }

        public IEnumerable<FantasyBlog.DAL.User> PageUsers()
        {
            var tempTable = db.UserSet.Skip((CurrentPage - 1) * PageSize).Take(PageSize);
            list = tempTable.ToList();
            return list;
        }

        public IEnumerable<FantasyBlog.DAL.User> SortUsers()
        {
            var tempTable = db.UserSet.OrderBy(SortBy, SortAscending);
            list = tempTable.ToList();
            return list;
        }

        public IEnumerable<FantasyBlog.DAL.User> list;

        public BlogModelContainer db = new BlogModelContainer();

        public string SortBy { get; set; }

        public bool SortAscending { get; set; }

        public int CurrentPage { get; set; }

        public int PageSize { get; set; }

        public int TotalRecordCount { get; set; }

        public int PageCount
        {
            get
            {
                if (TotalRecordCount % PageSize == 0)
                {
                    return TotalRecordCount / PageSize;
                }
                else
                {
                    return TotalRecordCount / PageSize + 1;
                }
            }
        }

        public string UserName { get; set; }

        public string UserStatus { get; set; }
    }

    public class UserWithRolesModel
    {
        public UserWithRolesModel(int id)
        {
            UserInfo = db.UserSet.Find(id);
            AllRoles = db.RoleSet.ToList();
            MyRoles = new Dictionary<FantasyBlog.DAL.Role, bool>();
            foreach (var r in AllRoles)
            {
                if (rp.IsUserInRole(UserInfo.username, r.name))
                {
                    MyRoles.Add(r, true);
                }
                else
                {
                    MyRoles.Add(r, false);
                }
            }
        }

        public User UserInfo { get; set; }

        public IEnumerable<FantasyBlog.DAL.Role> AllRoles { get; set; }

        public Dictionary<FantasyBlog.DAL.Role, bool> MyRoles { get; set; }

        public BlogModelContainer db = new BlogModelContainer();

        public BlogRoleProvider rp = new BlogRoleProvider();
    }
}